package tgc.rj.garbage;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import tgc.rj.garbage.custom.ToastUtils;
import tgc.rj.garbage.utlis.AisleUtils;
import tgc.rj.garbage.utlis.FilesUtils;
import tgc.rj.garbage.utlis.Msg;
import tgc.rj.garbage.utlis.SerialportCkUtils;

public class SettingActivity extends AppCompatActivity {
    private EditText qdjmc, gzdh, fwrx, fwqdz,shougd,weigd;
    public SharedPreferences sz;
    private Button btnbc, btnfh,btnqk;
    private String name, gzphone, fwphone, fwurl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        sz = this.getSharedPreferences("jcsz", MODE_PRIVATE);

        String name1 = sz.getString("name", "");
        String gzphone1 = sz.getString("gzphone", "");
        String fwphone1 = sz.getString("fwphone", "");
        String url1 = sz.getString("url", "https://baiyao.51baiyao.com");

        qdjmc = findViewById(R.id.qdjmc);
        qdjmc.setText(name1);
        gzdh = findViewById(R.id.gzdh);
        gzdh.setText(gzphone1);
        fwrx = findViewById(R.id.fwrx);
        fwrx.setText(fwphone1);
        fwqdz = findViewById(R.id.fwqdz);
        fwqdz.setText(url1);

        shougd = findViewById(R.id.shougd);
        weigd = findViewById(R.id.weigd);
        btnqk = findViewById(R.id.btnqk);
        btnqk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer shouNum = Integer.parseInt(shougd.getText().toString());
                Integer weiNum = Integer.parseInt(weigd.getText().toString());
                AisleUtils.deletetrack(shouNum,weiNum);
                ToastUtils.toastUtils(SettingActivity.this,"清空完成！");
            }
        });

        btnfh = findViewById(R.id.btnfh);
        btnfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnbc = findViewById(R.id.btnbc);
        btnbc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = qdjmc.getText().toString();
                gzphone = gzdh.getText().toString();
                fwphone = fwrx.getText().toString();
                fwurl = fwqdz.getText().toString();
                if (name.equals("") || name == null || gzphone.equals("") || gzphone == null || fwphone.equals("") || fwphone == null || fwurl.equals("") || fwurl == null) {
                    ToastUtils.toastUtils(SettingActivity.this, "信息不全，无法保存！");
                } else {
                    sendHttps(name);
                }
            }
        });
    }

    /***
     * 设置机器编号请求
     * @param name 机器编号
     * 请求名：weixin/jqbhyz
     */
    private void sendHttps(final String name) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                final String urls = sz.getString("url", "https://baiyao.51baiyao.com");
                final String urlStr = urls + "/gsm/weixin/jqbhyz";
                try {
                    URL url = new URL(urlStr);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    conn.setRequestProperty("Charset", "UTF-8");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setRequestMethod("POST");
                    String jqbhStr = URLEncoder.encode(name, "UTF-8");
                    final String paramStr = "jqbh=" + jqbhStr;
                    OutputStream out = conn.getOutputStream();
                    DataOutputStream dataOut = new DataOutputStream(out);
                    dataOut.writeBytes(paramStr);
                    dataOut.flush();
                    dataOut.close();
                    InputStream inputStream = conn.getInputStream();
                    final String reponse = FilesUtils.readInfo(inputStream);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Gson gson = new GsonBuilder().create();
                            Msg msg1 = gson.fromJson(reponse, Msg.class);
                            if (msg1.getSuccess()) {
                                SharedPreferences.Editor edit = sz.edit();
                                edit.putString("name", name);
                                edit.putString("gzphone", gzphone);
                                edit.putString("fwphone", fwphone);
                                edit.putString("url", fwurl);
                                edit.commit();
                                Toast.makeText(SettingActivity.this, "数据保存成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                ToastUtils.toastUtils(SettingActivity.this, msg1.getMsg());
                            }
                        }
                    });
                    if (conn != null) {
                        conn.disconnect();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
